#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/7/19  12:22
# @Author: 余浪人
# @email: yulangren520@gmail.com
import jieba
import jieba.analyse
from apps.control import sys_bp
from flask_login import login_required
from flask import request, render_template, jsonify
from apps.forms.content_form import TL_Content_Form, TL_Column_Form
from apps.lib.public import add_data, fake_delete_data, edit_data, check_obj, real_delete_data, recover_data, \
    real_deletes, fake_deletes, recover_datas, page_auto, add_tag
from apps.lib.token import Verify_authorization
from apps.models.content_model import db, TL_Column_model, TL_Content_Model

@sys_bp .route ('/content_add',endpoint ='content_add',methods =['GET','POST'])
@login_required
@Verify_authorization
def content_add ():
    if request .method =='POST':
        O00OOOOO0O0O0O00O =','.join (jieba .cut_for_search (request .form ['title']))if not request .form ['keyword']else None
        O00OOOOO0OO00O0O0 =[O0OOO000OO000O000 for O0OOO000OO000O000 ,_O0O0OO0OOOOO00000 in jieba .analyse .textrank (request .form ['title'],withWeight =True )]
        add_tag (O00OOOOO0OO00O0O0 )
        return add_data (TL_Content_Model ,TL_Content_Form ,db ,{'keyword':O00OOOOO0O0O0O00O })
    return render_template ('system_template/content.html',form =TL_Content_Form ())
@sys_bp .route ('/content_list',endpoint ='content_list',methods =['GET','DELETE','DELETES'])
@login_required
def content_list ():
    if request .method =='DELETE':
        return fake_delete_data (TL_Content_Model ,db ,request .form .get ('uid'))
    elif request .method =='DELETES':
        return fake_deletes (TL_Content_Model ,db ,request .form .get ('id_array').split ('#TL#'))
    O0O0O000O00OO0O0O =request .args .get ('search','')
    if O0O0O000O00OO0O0O :
        O00OOOOO0OO00OO0O ,OO00O000OO0OOOOO0 =page_auto (TL_Content_Model .query .order_by (TL_Content_Model .id .desc ()).filter (TL_Content_Model .status !=-1 ,TL_Content_Model .title .like (f"%{O0O0O000O00OO0O0O}%")))
    else :
        O00OOOOO0OO00OO0O ,OO00O000OO0OOOOO0 =page_auto (TL_Content_Model .query .order_by (TL_Content_Model .id .desc ()).filter (TL_Content_Model .status !=-1 ))
    return render_template ('system_template/content_list.html',data_list =O00OOOOO0OO00OO0O ,page =OO00O000OO0OOOOO0 )
@sys_bp .route ('/content_edit_<int:id>',endpoint ='content_edit',methods =['GET','POST'])
@login_required
def content_edit(id):
    O00O0O00O000O00OO =TL_Content_Model .query .filter_by (id =id ).first ()
    if request .method =='POST'and not check_obj (O00O0O00O000O00OO ):
        return edit_data (TL_Content_Form ,TL_Content_Model ,db ,id)
    if not check_obj (O00O0O00O000O00OO ):
        return render_template ('system_template/content.html',form =TL_Content_Form (data =dict (O00O0O00O000O00OO )))
@sys_bp .route ('/content_recycle_list',endpoint ='content_recycle_list',methods =['GET','DELETE','RECOVER','DELETES','RECOVERS'])
@login_required
def content_recycle_list ():
    if request .method =='DELETE':
        return real_delete_data (TL_Content_Model ,db ,request .form .get ('uid'))
    elif request .method =='RECOVER':
        return recover_data (TL_Content_Model ,db ,request .form .get ('uid'))
    elif request .method =='DELETES':
        return real_deletes (TL_Content_Model ,db ,request .form .get ('id_array').split ('#TL#'))
    elif request .method =='RECOVERS':
        return recover_datas (TL_Content_Model ,db ,request .form .get ('id_array').split ('#TL#'))
    OOOO00O0OO0O0OO0O ,O0OO000OOO0O0OOO0 =page_auto (TL_Content_Model .query .order_by (TL_Content_Model .id .desc ()).filter (TL_Content_Model .status ==-1 ))#line:71
    return render_template ('system_template/content_recycle_list.html',data_list =OOOO00O0OO0O0OO0O ,page =O0OO000OOO0O0OOO0 )
@sys_bp .route ('/column_add',endpoint ='column_add',methods =['GET','POST'])
@login_required
@Verify_authorization
def column_add ():#
    if request .method =='POST':
        return add_data (TL_Column_model ,TL_Column_Form ,db )
    return render_template ('system_template/column.html',form =TL_Column_Form ())
@sys_bp .route ('/column_list',endpoint ='column_list',methods =['GET','DELETE',"DELETES"])
@login_required
def column_list ():
    if request .method =='DELETE':
        OOOOOO00OO00O00O0 =request .form .get ('uid')
        if TL_Column_model .query .filter (TL_Column_model .column_parent_id ==OOOOOO00OO00O00O0 ).count ():
            return jsonify ({"status":1 ,"msg":"请先移除子栏目!"})
        elif TL_Content_Model .query .filter (TL_Content_Model .superior ==OOOOOO00OO00O00O0 ).count ():
            return jsonify ({"status":1 ,"msg":"请先移除栏目内容!(含:回收站)"})
        return real_delete_data (TL_Column_model ,db ,OOOOOO00OO00O00O0 )
    elif request .method =='DELETES':
        O00O0O0OO0OOO00OO =request .form .get ('id_array').split ('#TL#')
        for OOOOOO00OO00O00O0 in O00O0O0OO0OOO00OO :
            if TL_Column_model .query .filter (TL_Column_model .column_parent_id ==OOOOOO00OO00O00O0 ).count ():
                return jsonify ({"status":1 ,"msg":"请先移除子栏目!"})
            elif TL_Content_Model .query .filter (TL_Content_Model .superior ==OOOOOO00OO00O00O0 ).count ():
                return jsonify ({"status":1 ,"msg":"请先移除栏目内容!(含:回收站)"})
            return real_delete_data (TL_Column_model ,db ,OOOOOO00OO00O00O0 )
    O00000O00OOOO000O ,OO00OOOOO00OO000O =page_auto (TL_Column_model .query .order_by (TL_Column_model .id .desc ()))
    return render_template ('system_template/column_list.html',data_list =O00000O00OOOO000O ,page =OO00OOOOO00OO000O )
@sys_bp.route ('/column_edit<int:id>',endpoint ='column_edit',methods =['GET','POST'])#line:107
@login_required
def column_edit (id ):
    OOO00O000OO0O0OO0 =TL_Column_model .query .filter_by (id =id ).first ()
    if request .method =='POST'and not check_obj (OOO00O000OO0O0OO0 ):
        return edit_data (TL_Column_Form ,TL_Column_model ,db ,id )
    if not check_obj (OOO00O000OO0O0OO0 ):
        return render_template ('system_template/column.html',form =TL_Column_Form (data =dict (OOO00O000OO0O0OO0 )))
